package com.log4jviewer.ui.views;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class represents a handler for 'Start Server' button.
 * 
 * @author <a href="mailto:rd.ryly@gmail.com">Ruslan Diachenko</a>
 */
public class StartServerAction implements IViewActionDelegate {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private LogView logView;

    @Override
    public void run(final IAction action) {
        logView.startServer();
        logView.setStartActionState(false);
        logView.setStopActionState(true);
    }

    @Override
    public void selectionChanged(final IAction action, final ISelection selection) {
        logger.debug("Start Server action: Selection Changed!!");

        // Update server action state after selection in log table
        if (logView.isServerAlive()) {
            logView.setStartActionState(false);
        } else {
            logView.setStartActionState(true);
        }
    }

    @Override
    public void init(final IViewPart view) {
        this.logView = (LogView) view;
    }
}
